﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace _EF返回DateSet
{
    public class TableToEntity
    {
        public static List<Dictionary<string, object>> GetEntity(DataTable dt)
        {
            //返回json 需要引用json.net/Newtonsoft.Json 这两个其中一个dll
            string json = JsonConvert.SerializeObject(dt);
            
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            //表示数据量大于0
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Columns.Count > 0)
                    {

                        Dictionary<string, object> row = new Dictionary<string, object>();
                        ///获取所有列名 item.ColumnsName
                        foreach (DataColumn item in dt.Columns)
                        {
                            row.Add(item.ColumnName, dt.Rows[i][item.ColumnName]);
                        }
                        rows.Add(row);
                    }
                }
            }
            return rows;
        }
    }
}
